﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CDB.LinqModel.Data;
using CDB.LinqModel;
using CDB.LinqData;
using System.ComponentModel;
using System.Data.Linq.Mapping;

namespace CDB.LinqData
{
    public class DataRepository : IDataRepository
    {
        protected CDBDbContext _dbCDB;

        public DataRepository()
        {
            _dbCDB = CDBDatabaseFactory.CreateDbContext();
        }
        private void PopulateModel(object sourceObj, object targetObj)
        {
            var sourceProperties = TypeDescriptor.GetProperties(sourceObj).Cast<PropertyDescriptor>()
                .Where(p => p.Attributes.OfType<ColumnAttribute>().Count() > 0);

            var targetProperties = TypeDescriptor.GetProperties(targetObj).Cast<PropertyDescriptor>()
                .Where(p => p.Attributes.OfType<ColumnAttribute>().Count() > 0);


            for (int i = 0; i < sourceProperties.Count(); i++)
            {
                targetProperties.ElementAt(i).SetValue(targetObj,
                    sourceProperties.ElementAt(i).GetValue(sourceObj));
            }
        }

        #region Menu
        public void CreateMenu(Menus menu)
        {
            _dbCDB.Menus.InsertOnSubmit(menu);
            _dbCDB.SubmitChanges();
        }
        public void UpdateMenu(Menus menu)
        {
            Menus updateObj = (from m in _dbCDB.Menus
                               where m.Id.Equals(menu.Id)
                               select m).FirstOrDefault();
            PopulateModel(menu, updateObj);
            _dbCDB.SubmitChanges();
        }
        public void DeleteMenu(Menus menu)
        {
            Menus deleteObj = (from m in _dbCDB.Menus
                               where m.Id.Equals(menu.Id)
                               select m).First();
            _dbCDB.Menus.DeleteOnSubmit(deleteObj);
            PopulateModel(menu, deleteObj);
            _dbCDB.SubmitChanges();
        }
        public List<Menus> GetMenuNameByParentId(Menus menu)
        {
            List<Menus> objlstMenu = new List<Menus>();
            var result = (from m in _dbCDB.Menus
                          join m1 in _dbCDB.Menus on m.ParentId equals m1.Id
                          where m.ParentId == menu.ParentId
                          select m).ToList();
            if (result.Count() > 0)
                objlstMenu = result.ToList();

            return objlstMenu;
        }
        public List<Menus> GetMenuNameById(Menus menu)
        {
            var objlstMenu = (from m in _dbCDB.Menus
                              where m.IsParent.Equals(true)
                              select m).ToList();

            return objlstMenu;
        }
        public List<Menus> GetParentMenus()
        {
            List<Menus> objlstMenu = new List<Menus>();
            var result = (from m in _dbCDB.Menus
                          where m.ParentId == null
                          select m).ToList();
            if (result.Count() > 0)
                objlstMenu = result.ToList();

            return objlstMenu;
        }
        public void GetMenuById(Menus menu)
        {
            var result = (from m in _dbCDB.Menus
                          where m.Id.Equals(menu.Id)
                          select m).FirstOrDefault();

            if (result != null)
                PopulateModel(result, menu);
        }
        public List<Menus> GetAllMenus()
        {
            var objMenu = (from m in _dbCDB.Menus
                           select m).ToList();

            return objMenu;
        }
        public void GetMenuByName(Menus menu)
        {
            var objlstMenu = (from m in _dbCDB.Menus
                              where m.Name.Equals(menu.Name)
                              select m).SingleOrDefault();

            PopulateModel(objlstMenu, menu);
        }
        #endregion

        #region Content
        public void CreateContent(Content content)
        {
            _dbCDB.Contents.InsertOnSubmit(content);
            _dbCDB.SubmitChanges();
        }
        public void UpdateContent(Content content)
        {
            Content updateObj = (from c in _dbCDB.Contents
                                 where c.Id.Equals(content.Id)
                                 select c).FirstOrDefault();

            PopulateModel(content, updateObj);
            _dbCDB.SubmitChanges();
        }
        public void DeleteContent(Content content)
        {
            Content deleteObj = (from c in _dbCDB.Contents
                                 where c.Id.Equals(content.Id)
                                 select c).First();
            _dbCDB.Contents.DeleteOnSubmit(deleteObj);
            PopulateModel(content, deleteObj);
            _dbCDB.SubmitChanges();
        }
        public void getContentByPageName(Content content)
        {
            Content result = (from c in _dbCDB.Contents
                              where c.PageName.Equals(content.PageName)
                              select c).FirstOrDefault();

            if (result != null)
                PopulateModel(result, content);
        }
        public void getContentById(Content content)
        {
            Content result = (from s in _dbCDB.Contents
                              where s.Id.Equals(content.Id)
                              select s).FirstOrDefault();
            if (result != null)
                PopulateModel(result, content);
        }
        public List<Content> getAllContents()
        {
            return (from c in _dbCDB.Contents
                    select c).ToList();
        }
        #endregion

        #region Architect
        public void CreateArchitect(Architect architect)
        {
            _dbCDB.Architects.InsertOnSubmit(architect);
            _dbCDB.SubmitChanges();
        }
        public void UpdateArchitect(Architect architect)
        {
            Architect updateObj = (from a in _dbCDB.Architects
                                   where a.Id.Equals(architect.Id)
                                   select a).FirstOrDefault();
            PopulateModel(architect, updateObj);
            _dbCDB.SubmitChanges();
        }
        public List<Architect> GetAllArchitects()
        {
            return (from a in _dbCDB.Architects
                    select a).ToList();
        }
        public void DeleteArchitect(Architect architect)
        {
            //DeleteWorksByArchitectId(architect.Id);
            //DeleteFirmsByArchitectId(architect.Id);

            Architect deleteObj = (from a in _dbCDB.Architects
                                   where a.Id.Equals(architect.Id)
                                   select a).First();
            _dbCDB.Architects.DeleteOnSubmit(deleteObj);
            _dbCDB.SubmitChanges();
        }
        public void GetArchitectById(Architect architect)
        {
            Architect result = (from a in _dbCDB.Architects
                                where a.Id.Equals(architect.Id)
                                select a).FirstOrDefault();
            if (result != null)
                PopulateModel(result, architect);
        }
        public void GetArchitectByUsername(Architect architect)
        {
            Architect result = (from a in _dbCDB.Architects
                                where a.Username.Equals(architect.Username)
                                select a).FirstOrDefault();
            if (result != null)
                PopulateModel(result, architect);
        }
        //public List<Architect> GetArchitectsByCity(int cityId)
        //{
            //return (from a in _dbCDB.Architects where a.CityId.Equals(cityId) select a).ToList();
        //}
        public List<Architect> GetArchitectsByLocation(string location)
        {
            return (from a in _dbCDB.Architects where a.PropertyLocation.Equals(location) select a).ToList();
        }
        #endregion

        #region Architectural Works
        public List<ArchitecturalWork> GetAllWorksByArchitectId(ArchitecturalWork aWork)
        {
            return (from aw in _dbCDB.ArchitecturalWorks
                    where aw.ArchitectId.Equals(aWork.ArchitectId)
                    select aw).ToList();
        }
        public void DeleteWorksByArchitectId(int architectId)
        {
            List<ArchitecturalWork> deleteaWorks = (from aw in _dbCDB.ArchitecturalWorks
                                                    where aw.ArchitectId.Equals(architectId)
                                                    select aw).ToList();
            _dbCDB.ArchitecturalWorks.DeleteAllOnSubmit(deleteaWorks);
            _dbCDB.SubmitChanges();
        }
        public void CreateArchitecturalWorks(ArchitecturalWork architectWork)
        {
            _dbCDB.ArchitecturalWorks.InsertOnSubmit(architectWork);
            _dbCDB.SubmitChanges();
        }
        #endregion

        #region Builder Works
        public List<BuilderWork> GetAllWorksByBuilderId(BuilderWork aWork)
        {
            return (from aw in _dbCDB.BuilderWorks
                    where aw.BuilderId.Equals(aWork.BuilderId)
                    select aw).ToList();
        }
        public void DeleteWorksByBuilderId(int builderId)
        {
            List<BuilderWork> deletebWorks = (from aw in _dbCDB.BuilderWorks
                                                    where aw.BuilderId.Equals(builderId)
                                                    select aw).ToList();
            _dbCDB.BuilderWorks.DeleteAllOnSubmit(deletebWorks);
            _dbCDB.SubmitChanges();
        }
        public void CreateBuilderWorks(BuilderWork bWork)
        {
            _dbCDB.BuilderWorks.InsertOnSubmit(bWork);
            _dbCDB.SubmitChanges();
        }
        #endregion

        #region User
        public void CreateUser(User user)
        {
            _dbCDB.Users.InsertOnSubmit(user);
            _dbCDB.SubmitChanges();
        }
        public void UpdateUser(User user)
        {
            User updateObj = (from u in _dbCDB.Users
                              where u.Id.Equals(user.Id)
                              select u).FirstOrDefault();
            PopulateModel(user, updateObj);
            _dbCDB.SubmitChanges();
        }
        public void UpdateUserProfile(User user)
        {
            User updateObj = (from u in _dbCDB.Users
                              where u.Id.Equals(user.Id)
                              select u).FirstOrDefault();

            updateObj.FirstName = user.FirstName;
            updateObj.LastName = user.LastName;
            updateObj.Email = user.Email;
            updateObj.Phone = user.Phone;
            updateObj.Address1 = user.Address1;
            updateObj.Address2 = user.Address2;
            updateObj.Country = user.Country;
            updateObj.State = user.State;
            updateObj.City = user.City;
            updateObj.Zip = user.Zip;
            updateObj.PropertyLocation = user.PropertyLocation;
            updateObj.ServiceRequired = user.ServiceRequired;
            updateObj.UserName = user.UserName;
            updateObj.Password = user.Password;

            _dbCDB.SubmitChanges();
        }
        public void DeleteUser(User user)
        {
            User deleteObj = (from u in _dbCDB.Users
                              where u.Id.Equals(user.Id)
                              select u).First();
            _dbCDB.Users.DeleteOnSubmit(deleteObj);
            PopulateModel(user, deleteObj);
            _dbCDB.SubmitChanges();
        }
        public List<User> GetAllUsers()
        {
            var objuser = (from u in _dbCDB.Users
                           select u).ToList();

            return objuser;
        }
        public void GetUserById(User user)
        {
            User result = (from e in _dbCDB.Users
                           where e.Id.Equals(user.Id)
                           select e).FirstOrDefault();

            if (result != null)
                PopulateModel(result, user);
        }
        public void GetUserByUserName(User user)
        {
            User result = (from u in _dbCDB.Users
                           where u.UserName.Equals(user.UserName) || u.Email.Equals(user.Email)
                           select u).FirstOrDefault();

            if (result != null)
                PopulateModel(result, user);
        }
        public void GetUserByUniqueId(User user)
        {
            User result = (from u in _dbCDB.Users
                           where u.UniqueId.Equals(user.UniqueId)
                           select u).FirstOrDefault();

            if (result != null)
                PopulateModel(result, user);
        }
        public void ActivateUser(User user)
        {
            User updateObj = (from u in _dbCDB.Users
                              where u.UniqueId.Equals(user.UniqueId)
                              select u).FirstOrDefault();
            updateObj.IsActive = true;
            _dbCDB.SubmitChanges();
        }
        public bool checkUserLogin(User user)
        {
            var result = (from u in _dbCDB.Users
                          where u.UserName.Equals(user.UserName) &&
                          u.Password.Equals(user.Password) &&
                          u.IsActive == true
                          select u).SingleOrDefault();

            if (result != null)
                return true;
            else
                return false;
        }
        public void UpdateBuilderId(User user)
        {
            User updateObj = (from u in _dbCDB.Users
                              where u.Id.Equals(user.Id)
                              select u).FirstOrDefault();

            updateObj.BuilderId = user.BuilderId;

            _dbCDB.SubmitChanges();
        }
        public void UpdateArchitectId(User user)
        {
            User updateObj = (from u in _dbCDB.Users
                              where u.Id.Equals(user.Id)
                              select u).FirstOrDefault();

            updateObj.ArchitectId = user.ArchitectId;

            _dbCDB.SubmitChanges();
        }
        #endregion

        #region Builder
        public void CreateBuilder(Builder builder)
        {
            _dbCDB.Builders.InsertOnSubmit(builder);
            _dbCDB.SubmitChanges();
        }
        public void UpdateBuilder(Builder builder)
        {
            Builder updateObj = (from b in _dbCDB.Builders
                                 where b.Id.Equals(builder.Id)
                                 select b).FirstOrDefault();
            PopulateModel(builder, updateObj);
            _dbCDB.SubmitChanges();
        }
        public void DeleteBuilder(Builder builder)
        {
            Builder deleteObj = (from b in _dbCDB.Builders
                                 where b.Id.Equals(builder.Id)
                                 select b).First();
            _dbCDB.Builders.DeleteOnSubmit(deleteObj);
            PopulateModel(builder, deleteObj);
            _dbCDB.SubmitChanges();
        }
        public void CheckBuilderByUsername(Builder builder)
        {
            Builder result = (from b in _dbCDB.Builders
                              where b.Username.Contains(builder.Username) //&& b.IsActive == true
                              select b).FirstOrDefault();

            if (result != null)
                PopulateModel(result, builder);
        }
        public List<Builder> GetAllBuilders(Builder builder)
        {
            var objbuilder = (from b in _dbCDB.Builders
                              select b).ToList();
            return objbuilder;
        }
        public void GetBuilderById(Builder builder)
        {
            Builder result = (from b in _dbCDB.Builders
                              where b.Id.Equals(builder.Id)
                              select b).FirstOrDefault();
            if (result != null)
                PopulateModel(result, builder);
        }
        //public List<Builder> GetBuildersByCity(int cityId)
        //{
        //    return (from b in _dbCDB.Builders
        //            where b.CityId.Equals(cityId) select b).ToList();
        //}
        public List<Builder> GetBuildersByLocation(string location)
        {
            return (from b in _dbCDB.Builders
                    where b.PropertyLocation.Equals(location)
                    select b).ToList();
        }
        #endregion

        #region Country
        public List<Country> GetAllCountries()
        {
            return (from c in _dbCDB.Countries
                    select c).ToList();
        }
        public List<State> GetStatesByCountryId(int countryId)
        {
            return (from s in _dbCDB.States
                    where s.CountryId.Equals(countryId)
                    select s).ToList();
        }
        public Country GetCountryById(int countryId)
        {
            return (from c in _dbCDB.Countries
                    where c.Id.Equals(countryId)
                    select c).SingleOrDefault();
        }
        #endregion

        #region State
        public List<State> GetAllStates()
        {
            List<State> objState = new List<State>();
            objState = (from s in _dbCDB.States
                        select s).ToList();

            return objState;
        }
        public State GetStateById(int stateId)
        {
            return (from s in _dbCDB.States
                    where s.Id.Equals(stateId)
                    select s).SingleOrDefault();
        }
        #endregion

        #region City
        public List<City> GetCitiesByStateId(int stateId)
        {
            return (from c in _dbCDB.Cities
                    join s in _dbCDB.States on c.StateId equals s.Id
                    select c).ToList();
        }
        public List<City> GetAllCities()
        {
            return (from c in _dbCDB.Cities
                    select c).ToList();
        }
        //public List<City> GetCitiesByBuilders()
        //{
        //    return _dbCDB.ExecuteQuery<City>("select * from Cities where Id in (select distinct CityId from Builders)").ToList();
        //}
        //public List<City> GetCitiesByArchitects()
        //{
        //    return _dbCDB.ExecuteQuery<City>("select * from Cities where Id in (select distinct CityId from Architects)").ToList();
        //}
        #endregion

        #region Banner
        public void CreateBanner(Banner sBanner)
        {
            _dbCDB.Banners.InsertOnSubmit(sBanner);
            _dbCDB.SubmitChanges();
        }
        public void UpdateBanner(Banner sBanner)
        {
            Banner updateObj = (from sc in _dbCDB.Banners
                                where sc.Id.Equals(sBanner.Id)
                                select sc).FirstOrDefault();
            PopulateModel(sBanner, updateObj);
            _dbCDB.SubmitChanges();
        }
        public void DeleteBanner(Banner sBanner)
        {
            Banner deleteObj = (from du in _dbCDB.Banners
                                where du.Id.Equals(sBanner.Id)
                                select du).First();


            _dbCDB.Banners.DeleteOnSubmit(deleteObj);
            PopulateModel(sBanner, deleteObj);
            _dbCDB.SubmitChanges();
        }
        public void GetBannerById(Banner sBanner)
        {
            var result = (from sc in _dbCDB.Banners
                          where sc.Id.Equals(sBanner.Id)
                          select sc).First();

            if (result != null)
                PopulateModel(result, sBanner);
        }
        public List<Banner> GetAllBanners()
        {
            List<Banner> objBanner = new List<Banner>();
            objBanner = (from bn in _dbCDB.Banners
                         select bn).ToList();

            return objBanner;
        }
        public List<Banner> GetAllBannersByTypeID(int ImageType)
        {
            List<Banner> objBanner = new List<Banner>();
            objBanner = (from bn in _dbCDB.Banners
                         where bn.ImageType.Equals(ImageType)
                         select bn).ToList();

            return objBanner;
        }
        public Boolean GetVolusionNEmailId(Banner sBanner)
        {
            var result = (from sc in _dbCDB.Banners
                          where sc.BannerName.Equals(sBanner.BannerName)
                          select sc).ToList();

            if (result.Count > 0)
                return false;
            else
                return true;

        }
        public void GetBannerByBannerName(Banner banner)
        {
            var result = (from sc in _dbCDB.Banners
                          where sc.BannerName.Equals(banner.BannerName)
                          select sc).SingleOrDefault();

            if (result != null)
                PopulateModel(result, banner);
        }
        #endregion

        #region Subscription
        public void CreateSubscription(Subscription sub)
        {
            _dbCDB.Subscriptions.InsertOnSubmit(sub);
            _dbCDB.SubmitChanges();
        }
        public void GetSubscriptionByEmail(Subscription sub)
        {
            var result = (from n in _dbCDB.Subscriptions
                          where n.Email.Equals(sub.Email)
                          select n).FirstOrDefault();

            if (result != null)
                PopulateModel(result, sub);
        }
        public List<Subscription> GetAllSubscriptions()
        {
            return (from s in _dbCDB.Subscriptions
                    select s).ToList();
        }
        #endregion

        #region News Letter
        public void CreateNewsLetter(NewsLetter newsLetter)
        {
            _dbCDB.NewsLetters.InsertOnSubmit(newsLetter);
            _dbCDB.SubmitChanges();
        }
        public void DeleteNewsLetter(NewsLetter newsLetter)
        {
            NewsLetter deleteObj = (from n in _dbCDB.NewsLetters
                                    where n.Id.Equals(newsLetter.Id)
                                    select n).First();


            _dbCDB.NewsLetters.DeleteOnSubmit(deleteObj);
            PopulateModel(newsLetter, deleteObj);
            _dbCDB.SubmitChanges();
        }
        public void UpdateNewsLetterById(NewsLetter newsLetter)
        {
            NewsLetter updateNewsLInfo = (from n in _dbCDB.NewsLetters
                                          where n.Id.Equals(newsLetter.Id)
                                          select n).FirstOrDefault();

            if (updateNewsLInfo != null)
            {
                updateNewsLInfo.Subject = newsLetter.Subject;
                updateNewsLInfo.Content = newsLetter.Content;
            }
            _dbCDB.SubmitChanges();
        }
        public List<NewsLetter> GetAllNewsLetter()
        {
            return (from n in _dbCDB.NewsLetters
                    select n).ToList();
        }
        public void GetNewsLetterById(NewsLetter newsLetter)
        {
            var result = (from n in _dbCDB.NewsLetters
                          where n.Id.Equals(newsLetter.Id)
                          select n).FirstOrDefault();

            if (result != null)
                PopulateModel(result, newsLetter);
        }
        public void GetNewsLetterByTitle(NewsLetter newsLetter)
        {
            var result = (from n in _dbCDB.NewsLetters
                          where n.Subject.Equals(newsLetter.Subject)
                          select n).FirstOrDefault();

            if (result != null)
                PopulateModel(result, newsLetter);
        }
        #endregion

        #region News
        public void CreateNews(New news)
        {
            _dbCDB.News.InsertOnSubmit(news);
            _dbCDB.SubmitChanges();
        }
        public void UpdateNews(New news)
        {
            New updateNewsInfo = (from n in _dbCDB.News
                                          where n.Id.Equals(news.Id)
                                          select n).FirstOrDefault();

            if (updateNewsInfo != null)
            {
                updateNewsInfo.Title = news.Title;
                updateNewsInfo.Description = news.Description;
            }
            _dbCDB.SubmitChanges();
        }
        public void DeleteNews(New news)
        {
            New deleteObj = (from n in _dbCDB.News
                                    where n.Id.Equals(news.Id)
                                    select n).First();


            _dbCDB.News.DeleteOnSubmit(deleteObj);
            PopulateModel(news, deleteObj);
            _dbCDB.SubmitChanges();
        }
        public List<New> GetAllNews()
        {
            return (from n in _dbCDB.News
                    select n).ToList();
        }
        public void GetNewsByTitle(New news)
        {
            var result = (from n in _dbCDB.News
                          where n.Title.Equals(news.Title)
                          select n).FirstOrDefault();

            if (result != null)
                PopulateModel(result, news);
        }
        public void GetNewsById(New news)
        {
            var result = (from n in _dbCDB.News
                          where n.Id.Equals(news.Id)
                          select n).FirstOrDefault();

            if (result != null)
                PopulateModel(result, news);
        }
        #endregion

        #region Testimonial
        public void CreateTestimonial(Testimonial testimonial)
        {
            _dbCDB.Testimonials.InsertOnSubmit(testimonial);
            _dbCDB.SubmitChanges();
        }
        public void UpdateTestimonial(Testimonial testimonial)
        {
            Testimonial updateInfo = (from n in _dbCDB.Testimonials
                                  where n.Id.Equals(testimonial.Id)
                                  select n).FirstOrDefault();

            if (updateInfo != null)
            {
                updateInfo.Content = testimonial.Content;
                updateInfo.By = testimonial.By;
            }
            _dbCDB.SubmitChanges();
        }
        public void DeleteTestimonial(Testimonial testimonial)
        {
            Testimonial deleteObj = (from n in _dbCDB.Testimonials
                                     where n.Id.Equals(testimonial.Id)
                             select n).First();


            _dbCDB.Testimonials.DeleteOnSubmit(deleteObj);
            PopulateModel(testimonial, deleteObj);
            _dbCDB.SubmitChanges();
        }
        public List<Testimonial> GetAllTestimonials()
        {
            return (from n in _dbCDB.Testimonials
                    select n).ToList();
        }
        public void GetTestimonialById(Testimonial testimonial)
        {
            var result = (from n in _dbCDB.Testimonials
                          where n.Id.Equals(testimonial.Id)
                          select n).FirstOrDefault();

            if (result != null)
                PopulateModel(result, testimonial);
        }
        public void GetTestimonialByCreatedBy(Testimonial testimonial)
        {
            var result = (from n in _dbCDB.Testimonials
                          where n.By.Equals(testimonial.By)
                          select n).FirstOrDefault();

            if (result != null)
                PopulateModel(result, testimonial);
        }
        #endregion

        #region Property
        public void CreateProperty(Property property)
        {
            _dbCDB.Properties.InsertOnSubmit(property);
            _dbCDB.SubmitChanges();
        }
        public List<Property> GetAllPropertiesByUserId(int userId)
        {
            return (from p in _dbCDB.Properties
                    where p.UserId.Equals(userId)
                    select p).ToList();
        }
        public void GetPropertyById(Property property)
        {
            Property result = (from b in _dbCDB.Properties
                               where b.Id.Equals(property.Id)
                               select b).FirstOrDefault();

            if (result != null)
                PopulateModel(result, property);
        }
        public List<Property> GetAllProperties()
        {
            return (from p in _dbCDB.Properties
                    select p).ToList();
        }
        public void AssignBuilder(Property property)
        {
            Property updateInfo = (from p in _dbCDB.Properties
                                      where p.Id.Equals(property.Id)
                                      select p).FirstOrDefault();

            if (updateInfo != null)
            {
                updateInfo.BuilderId = property.BuilderId;
            }
            _dbCDB.SubmitChanges();
        }
        public void AssignArchitect(Property property)
        {
            Property updateInfo = (from p in _dbCDB.Properties
                                   where p.Id.Equals(property.Id)
                                   select p).FirstOrDefault();

            if (updateInfo != null)
            {
                updateInfo.ArchitectId = property.ArchitectId;
            }
            _dbCDB.SubmitChanges();
        }
        #endregion

        #region Property Images
        public void CreatePropertyImage(PropertyImage pImage)
        {
            _dbCDB.PropertyImages.InsertOnSubmit(pImage);
            _dbCDB.SubmitChanges();
        }
        public void CreatePropertyImages(List<PropertyImage> pImages)
        {
            _dbCDB.PropertyImages.InsertAllOnSubmit(pImages);
            _dbCDB.SubmitChanges();
        }
        public List<PropertyImage> GetAllPropertyImagesByPropertyId(int propertyId)
        {
            return (from p in _dbCDB.PropertyImages
                    where p.PropertyId.Equals(propertyId)
                    select p).ToList();
        }
        #endregion

        #region Blog
        public void CreateBlog(Blog blog)
        {
            _dbCDB.Blogs.InsertOnSubmit(blog);
            _dbCDB.SubmitChanges();
        }
        public void UpdateBlog(Blog blog)
        {
            Blog updateObj = (from b in _dbCDB.Blogs
                              where b.Id.Equals(blog.Id)
                              select b).FirstOrDefault();
            blog.DateAdded = updateObj.DateAdded;
            PopulateModel(blog, updateObj);
            _dbCDB.SubmitChanges();
        }
        public void DeleteBlog(Blog blog)
        {
            Blog deleteObj = (from b in _dbCDB.Blogs
                              where b.Id.Equals(blog.Id)
                              select b).First();
            _dbCDB.Blogs.DeleteOnSubmit(deleteObj);
            PopulateModel(blog, deleteObj);
            _dbCDB.SubmitChanges();
        }
        public List<Blog> GetAllBlogs()
        {
            var objblog = (from b in _dbCDB.Blogs
                           select b).ToList();

            return objblog;
        }
        public void GetBlogById(Blog blog)
        {
            Blog result = (from b in _dbCDB.Blogs
                           where b.Id.Equals(blog.Id)
                           select b).FirstOrDefault();

            if (result != null)
                PopulateModel(result, blog);
        }
        public void GetContentByBlogName(Blog blog)
        {
            Blog result = (from b in _dbCDB.Blogs
                           where b.Name.Equals(blog.Name)
                           select b).FirstOrDefault();

            if (result != null)
                PopulateModel(result, blog);
        }
        #endregion

        #region Payment History
        public void CreatePaymentHistory(PaymentHistory pHistory)
        {
            _dbCDB.PaymentHistories.InsertOnSubmit(pHistory);
            _dbCDB.SubmitChanges();
        }
        public List<PaymentHistory> GetAllPaymentsByUserId(PaymentHistory pHistory)
        {
            return (from ph in _dbCDB.PaymentHistories
                    where ph.UserId.Equals(pHistory.UserId)
                    select ph).ToList();
        }
        public List<PaymentHistory> GetAllPayments()
        {
            return (from ph in _dbCDB.PaymentHistories
                    select ph).ToList();
        }
        #endregion

        #region Bank Detail
        public void CreateBankDetail(BankDetail bDetail)
        {
            _dbCDB.BankDetails.InsertOnSubmit(bDetail);
            _dbCDB.SubmitChanges();
        }
        public void UpdateBankDetail(BankDetail bDetail)
        {
            BankDetail updateObj = (from b in _dbCDB.BankDetails
                                    where b.Id.Equals(bDetail.Id)
                                    select b).FirstOrDefault();
            PopulateModel(bDetail, updateObj);
            _dbCDB.SubmitChanges();
        }
        public void GetBankDetailByUserId(BankDetail bDetail)
        {
            BankDetail result = (from b in _dbCDB.BankDetails
                                    where b.UserId.Equals(bDetail.UserId)
                                    select b).FirstOrDefault();

            if (result != null)
                PopulateModel(result, bDetail);
        }
        #endregion
    }
}
